package com.rojored.controls.progressDisplayClasses 
{

import flash.display.CapsStyle;
import flash.events.Event;
import flash.display.LineScaleMode;
import mx.core.UIComponent;

/**
 *  Base class for drawing progress lines.
 *
 *  @author gabriel montagné láscaris-comneno gabriel@rojored.com 
 */
public class BaseProgressDisplay extends UIComponent 
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function BaseProgressDisplay() 
    {
        super();
    }

    //--------------------------------------------------------------------------
    //
    //  Variables.
    //
    //--------------------------------------------------------------------------

    /**
     *  Progress values, each in the range of 0--1.
     *
     *  <p>Values are assumed to be in order of prescendence where subsequent
     *  segments are drawn only for the length not coverred by previous
     *  segments.</p>
     */
    protected var progressValues:Vector.<Number> = Vector.<Number>([]);

    /**
     *  Progress colors. 
     */
    protected var progressColors:Vector.<uint> = Vector.<uint>([0xAAFF00, 0xFF0000]);

    /**
     *  Progress alphas.
     */
    protected var progressAlphas:Vector.<Number> = Vector.<Number>([0.3, 0.2]);

    //--------------------------------------------------------------------------
    //
    //  Properties 
    //
    //--------------------------------------------------------------------------

    //--------------------------------------
    //  drawTrack 
    //--------------------------------------

    /**
     *  @private
     *  Storage for the drawTrack property
     */
    private var _drawTrack:Boolean = true;

    /**
     *  @private
     */
    private var drawTrackChanged:Boolean;

    [Bindable("drawTrackChanged")]

    /**
     *  if <code>true</code> a padding track will be drawn after the last
     *  progress value up to the graphical end of the component.
     */
    public function get drawTrack():Boolean
    {
        return _drawTrack;
    }

    /**
     *  @private
     */
    public function set drawTrack(value:Boolean):void
    {
        if (_drawTrack == value)
            return;

        _drawTrack = value;
        drawTrackChanged = true;

        invalidateProperties();
        dispatchEvent(new Event("drawTrackChanged"));
    }

    //--------------------------------------
    //  trackColor 
    //--------------------------------------

    /**
     *  @private
     *  Storage for the trackColor property
     */
    private var _trackColor:uint = 0xFFAA00;

    /**
     *  @private
     */
    private var trackColorChanged:Boolean;

    [Bindable("trackColorChanged")]

    /**
     *  Color of the background track.
     */
    public function get trackColor():uint
    {
        return _trackColor;
    }

    /**
     *  @private
     */
    public function set trackColor(value:uint):void
    {
        if (_trackColor == value)
            return;

        _trackColor = value;
        trackColorChanged = true;
        invalidateDisplayList();
        dispatchEvent(new Event("trackColorChanged"));

    }

    //--------------------------------------
    //  trackAlpha 
    //--------------------------------------

    /**
     *  @private
     *  Storage for the trackAlpha property
     */
    private var _trackAlpha:Number = 0.5;

    /**
     *  @private
     */
    private var trackAlphaChanged:Boolean;

    [Bindable("trackAlphaChanged")]

    /**
     *  Alpha of the background track.
     */
    public function get trackAlpha():Number
    {
        return _trackAlpha;
    }

    /**
     *  @private
     */
    public function set trackAlpha(value:Number):void
    {
        if (_trackAlpha == value)
            return;

        _trackAlpha = value;
        trackAlphaChanged = true;

        invalidateProperties();

        dispatchEvent(new Event("trackAlphaChanged"));
    }

    //--------------------------------------------------------------------------
    //
    //  Overriden methods: UIComponent 
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    override protected function measure():void
    {
        super.measure();

        measuredWidth = 100;
        measuredHeight = 10;

    }

    /**
     *  @private
     */
    override protected function updateDisplayList(unscaledWidth:Number,
                                                  unscaledHeight:Number):void
    {
        super.updateDisplayList(unscaledWidth, unscaledHeight);
        drawProgress(unscaledWidth, unscaledHeight);
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     *  Does the actual drawing of the progress line based on the values
     *  collected in <code>progressValues</code>, <code>progressColors</code>
     *  and <code>progressAlphas</code>.
     *
     *  <p>Called in the context of <code>updateDisplayList</code>.</p>
     */
    protected function drawProgress(width:Number, height:Number):void
    {
        graphics.clear();

        var currentSegment:Number;
        var lastSegment:Number = 0;
        var segmentAlpha:Number;
        var segmentColor:uint;
        var segmentWidth:Number;
        var n:int = progressValues.length;
        for (var i:int = 0; i < n; i++)
        {
            currentSegment = progressValues[i];
            if (currentSegment <= lastSegment)
                continue;

            segmentWidth = (currentSegment - lastSegment) * width;

            if (!segmentWidth)
                continue;

            segmentColor = progressColors[i];
            segmentAlpha = progressAlphas[i];
            graphics.beginFill(segmentColor, segmentAlpha);
            graphics.drawRect(
                lastSegment * width,
                0,
                segmentWidth,
                height
            );
            lastSegment = currentSegment;
        }

        if (drawTrack && lastSegment < 1)
        {
            graphics.beginFill(trackColor, trackAlpha);
            graphics.drawRect(
                lastSegment * width,
                0,
                (1 - lastSegment) * width,
                height
            );
        }
    }
}
}
